﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class Stock_OutStock_Add : System.Web.UI.Page
{
    protected int OutStockKey;

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        OutStockKey = string.IsNullOrEmpty(Request["OutStockKey"]) ? 0 : Convert.ToInt32(Request["OutStockKey"]);

        if (!this.IsPostBack)
        {
            SalesType.Visible = true;
            OtherType.Visible = false;


            if (OutStockKey != 0)
            {
                Dat_InStock instock = new Dat_InStock();

                txt_Num.Text = instock.Num.ToString();

                btnSubmit.Text = "修 改";
            }
       }
    }

    //添加
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        TextBox txt_SalesCustomer = (TextBox)SalesType.FindControl("txt_SalesCustomer");
        TextBox txt_SalesPrice = (TextBox)SalesType.FindControl("txt_SalesPrice");
        TextBox txt_SalesAccountType = (TextBox)SalesType.FindControl("txt_SalesAccountType");
        TextBox txt_OtherReason = (TextBox)SalesType.FindControl("txt_OtherReason");

        if (OutStockKey == 0)
        {
                decimal? de = null;

                Dat_OutStock outstock = new Dat_OutStock
                {
                    DanNum = txt_DanNum.Text.Trim(),
                    OutStockNO = new OutStock().GetOutStockNO(),
                    StockKey = Convert.ToInt32(txt_StockKey.Value),
                    Num = Convert.ToInt32(txt_Num.Text),
                    OutType = txt_OutType.SelectedValue,
                    SalesCustomer = ( SalesType.Visible==false ? "" : txt_SalesCustomer.Text.Trim() ),
                    SalesPrice = (SalesType.Visible == false ? de : Convert.ToDecimal(txt_SalesPrice.Text)),
                    SalesAccountType = (SalesType.Visible == false ? "" : txt_SalesAccountType.Text.Trim()),
                    OtherReason = ( SalesType.Visible == true ? "" : txt_OtherReason.Text.Trim()),
                    CreateTime = DateTime.Now,
                    UserKey = Convert.ToInt32(Session["UserKey"])
                };


            if (new OutStock().AddOutStock(outstock))
            {
                WebUtility.JsAlertToGo("添加出库单成功", "OutStock_Add.aspx?menutype=331");
            }
        }
        else
        {
            Dat_OutStock outstock = new Dat_OutStock
            {
                OutStockKey = OutStockKey,
                Num = Convert.ToInt32(txt_Num.Text),
                OutType = txt_OutType.SelectedValue,
                SalesCustomer =   txt_SalesCustomer==null ? "": txt_SalesCustomer.Text.Trim() ,
                SalesPrice =  txt_SalesPrice == null ? (decimal?)Convert.DBNull : Convert.ToDecimal(txt_SalesPrice.Text.Trim()) ,
                SalesAccountType = txt_SalesAccountType == null ? "" : txt_SalesAccountType.Text.Trim(),
                OtherReason = txt_OtherReason == null ? "" : txt_OtherReason.Text.Trim()
            };

            if (new OutStock().EditOutStock(outstock))
            {
                WebUtility.JsAlertToGo("修改出库单成功", "OutStock_Add.aspx?menutype=331");
            }
        }
    }

    protected void txt_OutType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (txt_OutType.SelectedValue == "销售")
        {
            SalesType.Visible = true;
            OtherType.Visible = false;
        }
        else
        {
            SalesType.Visible = false;
            OtherType.Visible = true;
        }

    }
}
